﻿Imports System.Data.SqlClient

Public Class cDefendant

    Dim con As New SqlConnection(strcon)

    Public Function select_Denfendant_byID(ByVal PlaintiffID As Long) As DataTable
        Dim dt As New DataTable
        Dim oMain As New cMain
        dt = oMain.getData("SELECT * FROM tblDefendant WHERE PlaintiffID=N'" & PlaintiffID & "'")
        Return dt
    End Function

    Public Function select_Defendant_byCaseID(ByVal CaesID As Long) As DataTable
        Dim dt As New DataTable
        Dim oMain As New cMain
        dt = oMain.getData("SELECT * FROM tblDefendant WHERE CaseID=N'" & CaesID & "'")
        Return dt
    End Function

    Public Function insert_Defendant(ByVal DefendantName As String, ByVal Gender As String, ByVal DOB As Date, ByVal Address As String, ByVal Tel As String, ByVal Email As String, ByVal IDCard As String, ByVal Photo As Byte(), ByVal CaseID As Long, ByVal UserID As String) As Boolean
        Dim rv As Boolean = True
        Dim cmd As New SqlCommand
        cmd.CommandText = "INSERT INTO tblDefendant(DefendantName,Gender,DOB,Address,Tel,Email,IDCard,Photo,CaseID,UserID) VALUES(@DefendantName,@Gender,@DOB,@Address,@Tel,@Email,@IDCard,@Photo,@CaseID,@UserID)"
        With cmd.Parameters
            .AddWithValue("@DefendantName", DefendantName)
            .AddWithValue("@Gender", Gender)
            .AddWithValue("@DOB", DOB)
            .AddWithValue("@Address", Address)
            .AddWithValue("@Tel", Tel)
            .AddWithValue("@Email", Email)
            .AddWithValue("@IDCard", IDCard)
            If Photo IsNot Nothing Then
                .AddWithValue("@Photo", Photo)
            Else
                .Add("@Photo", SqlDbType.Image).Value = DBNull.Value
            End If
            .AddWithValue("@CaseID", CaseID)
            .AddWithValue("@UserID", UserID)
        End With
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            rv = False
        Finally
            con.Close()
            cmd.Dispose()
        End Try
        Return rv
    End Function

    Public Function delelte_Defendant(ByVal CaseID As Long) As Boolean
        Dim rv As Boolean = True
        Dim cmd As New SqlCommand
        cmd.CommandText = "DELETE FROM tblDefendant WHERE CaseID=@CaseID"
        With cmd.Parameters
            .AddWithValue("@CaseID", CaseID)
        End With
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            rv = False
        Finally
            con.Close()
            cmd.Dispose()
        End Try
        Return rv
    End Function

End Class
